﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using Top.Web;
using Top.Web.Api;
using Top.PointStore.Business;
using Top.PointStore.Business.Models;
using Top.PointStore.Web.Filters;
using Top.Web.Api.Filters;
using Top.Business.OAuth;
using Top.Core;
//using Top.PointStore.Filters;

namespace Top.PointStore.Controllers
{
    public class SystemController : ApiBase
    {
        public object Logout()
        {
            Top.Web.Api.WebApiContext.Current.WebApi.Session.Abandon();
            return Redirect("/OAuth/Login");
        }

        public object GetUser()
        {
            return User;
        }

        /// <summary>
        /// 根据账号获取所管理的城市
        /// </summary>
        /// <returns></returns>
        [Login]
        public object GetCityList()
        {
            return SystemManager.GetCityListByAdminKid(pUser.CrmUserID);
        }
        public static string ReadIndexHtml(string filePath)
        {

            string result = string.Empty;
            string url = HttpContext.Current.Server.MapPath(filePath); //"~/index.html"

            using (StreamReader sr = new StreamReader(url))
            {
                result = sr.ReadToEnd();
            }            
            return result;
        }

        /// <summary>
        /// 获取日志列表
        /// </summary>
        /// <param name="keyWords"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [Login]
        [Provider]
        public async Task<object> GetLogList(string keyWords, int pageIndex = 1, int pageSize = 20)
        {
            return await SystemManager.GetLogList(keyWords, Pager.Create(pageIndex, pageSize));
        }

        private string AllMenu = ReadIndexHtml("../admin/AllMenu.html");

        private string CityMenu = ReadIndexHtml("../admin/CityMenu.html");

        private string GysMenu = ReadIndexHtml("../admin/GysMenu.html");

        [NoLogin]
        public object GetMenu()
        {
            if (User==null)
            {
                return Redirect("/admin/login");
            }

            if (pUser.PointStoreAuth.F_AuthorityID == 1)
            {
                var result = HTML(AllMenu);
                return result.GetResponseMessage(this);
            }
            if (pUser.PointStoreAuth.F_AuthorityID == 3)
            {
                var result = HTML(CityMenu);
                return result.GetResponseMessage(this);
            }
            if (pUser.PointStoreAuth.F_AuthorityID==999)
            {
                var result = HTML(GysMenu);
                return result.GetResponseMessage(this);
            }
            return HTML("");
        }
        //[NoLogin]
        //public async Task PrjPointInfoByIDtoUK()
        //{
        //    //var hc = new HttpClient();
        //    var bids = await SystemManager.GetPointBrokerKids();
        //    //const string urltlp = @"{0}/User/GetBrokerToken?brokerkid={1}";
        //    foreach (int bid in bids)
        //    {
        //        Guid uk = default(Guid);
        //        try
        //        {
        //            //var url = new Uri(string.Format(urltlp, Configs.AppSettings["oauth.domain"], bid));
        //            //var sr = await hc.GetStringAsync(url);
        //            uk = await OAuth.GetBrokerToken(bid); //JsonConvert.DeserializeObject<ResultModel<string>>(sr).Data;
        //            if (uk != null && uk != default(Guid))
        //            {
        //                await SystemManager.PrjPointInfoByIDtoUK(bid, uk.ToString());
        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            logger.Error(string.Format("borkerkid:{0}, uk:{1}, " + ex.Message, bid, uk));
        //        }
        //    }
        //}

        //private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
    }
}
